<template>
  <view class="medical-records-page">
    <!-- 添加按钮 -->
    <view class="add-btn" @tap="showAddModal">
      <text class="add-icon">+</text>
      <text class="add-text">添加病例</text>
    </view>

    <!-- 病例列表 -->
    <view class="records-list">
      <view 
        v-for="record in records" 
        :key="record.id"
        class="record-card"
        @tap="viewRecord(record)"
      >
        <view class="record-header">
          <view class="record-date">
            <text class="date-icon">📅</text>
            <text class="date-text">{{ record.date }}</text>
          </view>
          <view class="record-actions">
            <view class="action-btn" @tap.stop="editRecord(record)">
              <text>✏️</text>
            </view>
            <view class="action-btn" @tap.stop="deleteRecord(record.id)">
              <text>🗑️</text>
            </view>
          </view>
        </view>
        
        <view class="record-content">
          <view class="record-item">
            <text class="item-label">医院：</text>
            <text class="item-value">{{ record.hospital }}</text>
          </view>
          <view class="record-item">
            <text class="item-label">科室：</text>
            <text class="item-value">{{ record.department }}</text>
          </view>
          <view class="record-item">
            <text class="item-label">诊断：</text>
            <text class="item-value">{{ record.diagnosis }}</text>
          </view>
          <view v-if="record.doctor" class="record-item">
            <text class="item-label">医生：</text>
            <text class="item-value">{{ record.doctor }}</text>
          </view>
        </view>
      </view>

      <view v-if="records.length === 0" class="empty-state">
        <text class="empty-icon">📋</text>
        <text class="empty-text">暂无病例记录</text>
        <text class="empty-hint">点击上方按钮添加病例</text>
      </view>
    </view>

    <!-- 添加/编辑弹窗 -->
    <view v-if="showModal" class="modal-overlay" @tap="closeModal">
      <view class="modal-content" @tap.stop>
        <view class="modal-header">
          <text class="modal-title">{{ isEdit ? '编辑病例' : '添加病例' }}</text>
          <view class="modal-close" @tap="closeModal">
            <text>✕</text>
          </view>
        </view>
        
        <scroll-view class="modal-body" scroll-y>
          <view class="form-group">
            <text class="form-label">就诊日期</text>
            <picker mode="date" :value="formData.date" @change="onDateChange">
              <view class="picker-input">
                <text>{{ formData.date || '请选择日期' }}</text>
              </view>
            </picker>
          </view>

          <view class="form-group">
            <text class="form-label">医院名称</text>
            <input 
              v-model="formData.hospital" 
              class="form-input"
              placeholder="请输入医院名称"
            />
          </view>

          <view class="form-group">
            <text class="form-label">就诊科室</text>
            <input 
              v-model="formData.department" 
              class="form-input"
              placeholder="请输入科室"
            />
          </view>

          <view class="form-group">
            <text class="form-label">医生姓名</text>
            <input 
              v-model="formData.doctor" 
              class="form-input"
              placeholder="请输入医生姓名（可选）"
            />
          </view>

          <view class="form-group">
            <text class="form-label">诊断结果</text>
            <textarea 
              v-model="formData.diagnosis" 
              class="form-textarea"
              placeholder="请输入诊断结果"
              maxlength="500"
            />
          </view>

          <view class="form-group">
            <text class="form-label">症状描述</text>
            <textarea 
              v-model="formData.symptoms" 
              class="form-textarea"
              placeholder="请描述症状（可选）"
              maxlength="500"
            />
          </view>

          <view class="form-group">
            <text class="form-label">处方药物</text>
            <textarea 
              v-model="formData.prescription" 
              class="form-textarea"
              placeholder="请输入处方药物（可选）"
              maxlength="500"
            />
          </view>

          <!-- 添加图片上传功能 -->
          <view class="form-group">
            <text class="form-label">病例照片（可选）</text>
            <view class="image-upload-section">
              <view 
                v-for="(img, index) in formData.images" 
                :key="index"
                class="image-item"
              >
                <image :src="img" class="uploaded-image" mode="aspectFill" />
                <view class="image-delete" @tap="deleteImage(index)">
                  <text>✕</text>
                </view>
              </view>
              <view v-if="formData.images.length < 6" class="upload-btn-wrapper">
                <view class="upload-btn" @tap="chooseImage">
                  <text class="upload-icon">📷</text>
                  <text class="upload-text">上传照片</text>
                </view>
              </view>
            </view>
            <text class="form-hint">最多上传6张照片，可上传检查报告、处方单等</text>
          </view>
        </scroll-view>

        <view class="modal-footer">
          <view class="modal-btn cancel" @tap="closeModal">
            <text class="btn-text">取消</text>
          </view>
          <view class="modal-btn confirm" @tap="saveRecord">
            <text class="btn-text">保存</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue'

const records = ref([
  {
    id: 1,
    date: '2024-01-15',
    hospital: '市人民医院',
    department: '内科',
    doctor: '张医生',
    diagnosis: '急性上呼吸道感染',
    symptoms: '咳嗽、发热、咽痛',
    prescription: '阿莫西林胶囊、感冒灵颗粒'
  },
  {
    id: 2,
    date: '2024-02-20',
    hospital: '中医院',
    department: '体检中心',
    doctor: '李医生',
    diagnosis: '健康体检',
    symptoms: '无',
    prescription: '无'
  }
])

const showModal = ref(false)
const isEdit = ref(false)
const currentId = ref(null)

const formData = ref({
  date: '',
  hospital: '',
  department: '',
  doctor: '',
  diagnosis: '',
  symptoms: '',
  prescription: '',
  images: [] // 添加图片数组
})

const showAddModal = () => {
  isEdit.value = false
  formData.value = {
    date: new Date().toISOString().split('T')[0],
    hospital: '',
    department: '',
    doctor: '',
    diagnosis: '',
    symptoms: '',
    prescription: '',
    images: [] // 初始化图片数组
  }
  showModal.value = true
}

const closeModal = () => {
  showModal.value = false
}

const onDateChange = (e) => {
  formData.value.date = e.detail.value
}

const saveRecord = () => {
  if (!formData.value.hospital || !formData.value.diagnosis) {
    if (typeof wx !== 'undefined') {
      wx.showToast({
        title: '请填写必填项',
        icon: 'none'
      })
    }
    return
  }

  if (isEdit.value) {
    const index = records.value.findIndex(r => r.id === currentId.value)
    if (index !== -1) {
      records.value[index] = { ...formData.value, id: currentId.value }
    }
  } else {
    const newRecord = {
      ...formData.value,
      id: Date.now()
    }
    records.value.unshift(newRecord)
  }

  if (typeof wx !== 'undefined') {
    wx.showToast({
      title: '保存成功',
      icon: 'success'
    })
  }

  closeModal()
}

const editRecord = (record) => {
  isEdit.value = true
  currentId.value = record.id
  formData.value = { ...record }
  showModal.value = true
}

const deleteRecord = (id) => {
  if (typeof wx !== 'undefined') {
    wx.showModal({
      title: '确认删除',
      content: '确定要删除这条病例记录吗？',
      success: (res) => {
        if (res.confirm) {
          records.value = records.value.filter(r => r.id !== id)
          wx.showToast({
            title: '删除成功',
            icon: 'success'
          })
        }
      }
    })
  }
}

const viewRecord = (record) => {
  // 查看详情功能 - 暂不打开弹窗，可以后续添加详情页面
  console.log('[v0] View record:', record)
}

const chooseImage = () => {
  if (typeof wx !== 'undefined') {
    wx.chooseMedia({
      count: 6 - formData.value.images.length,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        const tempFiles = res.tempFiles.map(file => file.tempFilePath)
        formData.value.images = [...formData.value.images, ...tempFiles]
      }
    })
  }
}

const deleteImage = (index) => {
  formData.value.images.splice(index, 1)
}
</script>

<style scoped>
.medical-records-page {
  min-height: 100vh;
  background: #f5f5f5;
  padding: 32rpx;
}

.add-btn {
  background: linear-gradient(135deg, #10b981, #3b82f6);
  border-radius: 16rpx;
  padding: 32rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16rpx;
  margin-bottom: 32rpx;
  box-shadow: 0 4rpx 12rpx rgba(59, 130, 246, 0.3);
  transition: all 0.2s;
}

.add-btn:active {
  transform: scale(0.98);
}

.add-icon {
  font-size: 40rpx;
  color: white;
  font-weight: bold;
}

.add-text {
  font-size: 28rpx;
  color: white;
  font-weight: 500;
}

.records-list {
  display: flex;
  flex-direction: column;
  gap: 24rpx;
}

.record-card {
  background: white;
  border-radius: 16rpx;
  padding: 32rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
  transition: all 0.2s;
}

.record-card:active {
  transform: scale(0.98);
}

.record-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24rpx;
  padding-bottom: 16rpx;
  border-bottom: 1rpx solid #f3f4f6;
}

.record-date {
  display: flex;
  align-items: center;
  gap: 12rpx;
}

.date-icon {
  font-size: 28rpx;
}

.date-text {
  font-size: 28rpx;
  color: #6b7280;
  font-weight: 500;
}

.record-actions {
  display: flex;
  gap: 16rpx;
}

.action-btn {
  width: 56rpx;
  height: 56rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: #f3f4f6;
  font-size: 28rpx;
  transition: background-color 0.2s;
}

.action-btn:active {
  background: #e5e7eb;
}

.record-content {
  display: flex;
  flex-direction: column;
  gap: 16rpx;
}

.record-item {
  display: flex;
  gap: 16rpx;
}

.item-label {
  font-size: 28rpx;
  color: #6b7280;
  min-width: 120rpx;
}

.item-value {
  font-size: 28rpx;
  color: #1f2937;
  flex: 1;
}

.empty-state {
  text-align: center;
  padding: 120rpx 32rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16rpx;
}

.empty-icon {
  font-size: 96rpx;
  opacity: 0.3;
}

.empty-text {
  font-size: 32rpx;
  color: #6b7280;
  display: block;
}

.empty-hint {
  font-size: 24rpx;
  color: #9ca3af;
  display: block;
}

/* 弹窗样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 32rpx;
  box-sizing: border-box;
}

.modal-content {
  background: white;
  border-radius: 24rpx;
  width: 100%;
  max-width: 600rpx;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-sizing: border-box;
}

.modal-header {
  padding: 32rpx;
  border-bottom: 1rpx solid #e5e7eb;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-title {
  font-size: 32rpx;
  font-weight: 600;
  color: #1f2937;
}

.modal-close {
  width: 48rpx;
  height: 48rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background-color 0.2s;
  font-size: 32rpx;
  color: #6b7280;
}

.modal-close:active {
  background: #f3f4f6;
}

.modal-body {
  flex: 1;
  padding: 32rpx;
  overflow-y: auto;
  box-sizing: border-box;
}

.form-group {
  margin-bottom: 32rpx;
  box-sizing: border-box;
}

.form-label {
  font-size: 28rpx;
  color: #374151;
  display: block;
  margin-bottom: 12rpx;
  font-weight: 500;
}

/* 修复输入框宽度和高度问题 */
.form-input {
  width: 100%;
  height: 80rpx;
  padding: 0 24rpx;
  border: 1rpx solid #d1d5db;
  border-radius: 12rpx;
  font-size: 28rpx;
  background: #f9fafb;
  box-sizing: border-box;
  line-height: 80rpx;
}

.picker-input {
  width: 100%;
  height: 80rpx;
  padding: 0 24rpx;
  border: 1rpx solid #d1d5db;
  border-radius: 12rpx;
  font-size: 28rpx;
  background: #f9fafb;
  box-sizing: border-box;
  display: flex;
  align-items: center;
}

.picker-input text {
  color: #374151;
}

.form-textarea {
  width: 100%;
  padding: 20rpx 24rpx;
  border: 1rpx solid #d1d5db;
  border-radius: 12rpx;
  font-size: 28rpx;
  background: #f9fafb;
  min-height: 160rpx;
  box-sizing: border-box;
  line-height: 1.6;
}

/* 添加图片上传样式 */
.image-upload-section {
  display: flex;
  flex-wrap: wrap;
  gap: 16rpx;
}

.image-item {
  width: 160rpx;
  height: 160rpx;
  position: relative;
  border-radius: 12rpx;
  overflow: hidden;
}

.uploaded-image {
  width: 100%;
  height: 100%;
}

.image-delete {
  position: absolute;
  top: 8rpx;
  right: 8rpx;
  width: 40rpx;
  height: 40rpx;
  background: rgba(0, 0, 0, 0.6);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 24rpx;
}

.upload-btn-wrapper {
  width: 160rpx;
  height: 160rpx;
}

.upload-btn {
  width: 100%;
  height: 100%;
  border: 2rpx dashed #d1d5db;
  border-radius: 12rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8rpx;
  background: #f9fafb;
  transition: all 0.2s;
}

.upload-btn:active {
  background: #f3f4f6;
  border-color: #9ca3af;
}

.upload-icon {
  font-size: 48rpx;
}

.upload-text {
  font-size: 24rpx;
  color: #6b7280;
}

.form-hint {
  font-size: 24rpx;
  color: #9ca3af;
  display: block;
  margin-top: 8rpx;
}

.modal-footer {
  padding: 32rpx;
  border-top: 1rpx solid #e5e7eb;
  display: flex;
  gap: 16rpx;
}

.modal-btn {
  flex: 1;
  padding: 24rpx;
  border-radius: 12rpx;
  text-align: center;
  transition: all 0.2s;
}

.modal-btn.cancel {
  background: #f3f4f6;
}

.modal-btn.cancel:active {
  background: #e5e7eb;
}

.modal-btn.confirm {
  background: #3b82f6;
}

.modal-btn.confirm:active {
  background: #2563eb;
}

.btn-text {
  font-size: 28rpx;
  font-weight: 500;
  color: #374151;
}

.modal-btn.confirm .btn-text {
  color: white;
}
</style>
